/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import com.xy.biz.base.domain.bo.CorpBO;
import com.xy.biz.base.domain.converter.CorpConverter;
import com.xy.biz.base.domain.dto.CorpDTO;
import com.xy.biz.base.domain.query.CorpQuery;
import com.xy.biz.base.domain.req.GetCorpRequest;
import com.xy.biz.base.domain.req.ListCorpItemRequest;
import com.xy.biz.base.domain.req.ListCorpRequest;
import com.xy.biz.base.domain.req.SaveCorpRequest;
import com.xy.biz.base.service.CorpService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.domain.StringKeyValue;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * 
 * @author zsp
 * @date 2020-9-3
 */
@Slf4j
@RestController
@RequestMapping("/api/base/corp")
public class CorpController {

	@Autowired
	private CorpService corpService;
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveCorp(@RequestBody SaveCorpRequest request) {
		corpService.saveCorp(CorpConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateCorp(@RequestBody SaveCorpRequest request) {
		corpService.updateCorp(CorpConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteCorp(@RequestBody GetCorpRequest request) {
		corpService.deleteCorp(request.getCorpId());
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/get")
	public Result<CorpDTO> getCorp(@RequestBody GetCorpRequest request) {
		return Result.success(CorpConverter.toDTO(corpService.getCorp(request.getCorpId())));
	}

	@PostMapping(value = "/list_by_ids")
	public Result<List<CorpDTO>> listCorpById(@RequestBody Collection<Long> collection) {
		return Result.success(CorpConverter.toDTO(corpService.listCorpById(collection)));
	}

	@PostMapping(value = "/list")
	public Result<List<CorpDTO>> listCorp(@RequestBody ListCorpRequest request) {
		return Result.success(CorpConverter.toDTO(corpService.listCorp(CorpConverter.toQuery(request))));
	}

	@PostMapping(value = "/count")
	public Result<Integer> countCorp(@RequestBody ListCorpRequest request) {
		return Result.success(corpService.countCorp(CorpConverter.toQuery(request)));
	}

	@PostMapping(value = "/page")
	public Result<Page<CorpDTO>> pageCorp(@RequestBody ListCorpRequest request) {
		CorpQuery query = CorpConverter.toQuery(request);
		List<CorpDTO> list = CorpConverter.toDTO(corpService.listCorp(query));
		int count = corpService.countCorp(query);
		return Result.success(new Page<>(list, count));
	}

	@PostMapping(value = "/list_item")
	public Result<List<StringKeyValue>> listCorpItem(@RequestBody ListCorpItemRequest request) {
		List<CorpBO> corpBOList = corpService.listCorp(CorpQuery.max()
				.groupDictCode(request.getGroupDictCode())
				.provinceRegionId(request.getProvinceRegionId())
				.build());
		if(!CollectionUtils.isEmpty(corpBOList)) {
			return Result.success(corpBOList.stream().map(e -> {
				StringKeyValue stringKeyValue = new StringKeyValue();
				stringKeyValue.setKey(e.getCorpId().toString());
				stringKeyValue.setValue(e.getCorpName() + "(" + e.getCorpCode() + ")");
				return stringKeyValue;
			}).collect(Collectors.toList()));
		}
		return Result.success(Collections.emptyList());
	}
	
}
